package com.chery.media.library.extend

import android.graphics.Bitmap
import androidx.core.graphics.drawable.toBitmap
import coil.imageLoader
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.blankj.utilcode.util.Utils
import com.chery.media.library.R

/**
 *  @author: pandeng
 *  @className: BitmapExt
 *  @description:
 *  @date: 2022/12/25
 */

suspend fun getImageBitmapByUrl(url: Any, defaultImage: Int = R.drawable.ps_image_placeholder): Bitmap? {
    val request = ImageRequest.Builder(Utils.getApp())
        .data(url)
        .allowHardware(false)
        .build()
    val result = Utils.getApp().imageLoader.execute(request)
    return if (result is SuccessResult) {
        result.drawable.toBitmap()
    } else {
        defaultImage.toDrawable()?.toBitmap()
    }
}